package com.wangzhixuan.controller;

import com.wangzhixuan.db.bean.Customer;
import com.wangzhixuan.db.bean.Order;
import com.wangzhixuan.service.OrderService;
import com.wangzhixuan.util.HttpUtils;
import com.wangzhixuan.vo.JsonVo;
import com.wangzhixuan.vo.SpecialVo;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/")
public class OrderController  extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WxMpService wxMpService;
    @ResponseBody
    @RequestMapping(value = "/addOrder.json", method = RequestMethod.POST)
    public JsonVo<Order> addOrder(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,
                                   long doctorId, long typeId, String memo ) {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Customer currentCustomer = getCurrentCustomer(request);
        JsonVo<Order> vo=new JsonVo<Order>();
        Order order = new Order();
        order.setDoctorId(doctorId);
        order.setProductTypeId(typeId);
        order.setMemo(memo);
        long customerId=1;
        if(currentCustomer!=null){
            customerId=currentCustomer.getId();
        }
        order.setCustomerId(customerId);
        orderService.addOrder(order);
        vo.setResult(true);
        vo.setT(order);
        return vo;
    }

    @RequestMapping("/payOrder.htm")
    public String payOrder(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, String orderId ) {
        Customer currentCustomer = getCurrentCustomer(request);
        JsonVo<String> vo=new JsonVo<String>();

      //  String basePath = HttpUtils.getBasePath(request);
        WxJsapiSignature signature = null;
        try {
            signature = wxMpService.createJsapiSignature(HttpUtils.getFullURL(request));
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        System.out.println("payOrder : " + orderId);
        orderId=orderId.replace(",","");
        Order order =orderService.getOrderDetail(Long.parseLong(orderId));
        modelMap.put("order", order);
        modelMap.put("order_id",orderId+"");
        modelMap.put("signature", signature);
        modelMap.put("timestamp", signature.getTimestamp() + "");
        if(order.getType()==0){
            return "pay_order";
        }else{
            return "pay_orderSpecial";
        }
    }

    @RequestMapping("/detailOrder.htm")
    public String detailOrder(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,long orderId) {
        Order order =orderService.getOrderDetail(orderId);
        modelMap.put("order", order);
        if(order.getType()==0){
            return "order";
        }else{
            return "orderSpecial";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/cancelOrder.json", method = RequestMethod.GET)
    public JsonVo<String> cancelOrder(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,
                                      long orderId ) {
        JsonVo<String> vo=new JsonVo<String>();
        orderService.updateOrderStatus(orderId,-1);
        vo.setResult(true);
        vo.setT("");
        return vo;
    }
    @RequestMapping(value = "/special.htm",method = RequestMethod.GET)
    public String zdy(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        return "special";
    }
    @ResponseBody
    @RequestMapping(value="/addSpecial.json",method=RequestMethod.POST)
    public JsonVo<Order> addZdyOrder(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap,
                                      String nickname,String cellphone,String sex,String hospital,String dept,String memo){
        JsonVo<Order> vo=new JsonVo<Order>();
        try{
            request.setCharacterEncoding("utf-8");
            Customer currentCustomer = getCurrentCustomer(request);
            Long customerId=currentCustomer.getId();
            SpecialVo specialVo=new SpecialVo();
            specialVo.setName(nickname);
            specialVo.setCellphone(cellphone);
            specialVo.setDepartment(dept);
            specialVo.setHospital(hospital);
            specialVo.setMemo(memo);
            specialVo.setSex(sex);
            Order order=new Order();
            order.setCustomerId(customerId);
            order.setMemo(specialVo.toString());
            orderService.addSpecialOrder(order);
            vo.setResult(true);
            vo.setT(order);
        }catch(Exception e){
            vo.setResult(false);
            e.printStackTrace();
        }
        return vo;
    }
}
